﻿using MobileBSC_Data;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MobileBSC_SQLite
{
    public class GoalReferenceMapReader
    {
        private StrategicGoalReader GoalReader { get; set; }

        private const string SELECT_STMT =
            "SELECT " +
                 "GoalReferenceMapID " +
               ", BaseGoalID " +
               ", TargetGoalID " +
            "FROM GoalReferenceMap ";

        public GoalReferenceMapReader()
        {
            GoalReader = new StrategicGoalReader();
        }
        public GoalReferenceMap ReadByID(long id)
        {
            var statement = SELECT_STMT + "WHERE GoalReferenceMapID = @id;";
            var parameters = new List<SQLiteParameter>();
            parameters.Add(new SQLiteParameter("@id", id));

            return DbTool<GoalReferenceMap>.ReadSingle(statement, parameters, CreateGRM);
        }
        public List<GoalReferenceMap> ReadByBaseGoal(long goalId)
        {
            var statement = SELECT_STMT + "WHERE BaseGoalID = @id;";
            var parameters = new List<SQLiteParameter>();
            parameters.Add(new SQLiteParameter("@id", goalId));

            return DbTool<GoalReferenceMap>.ReadMultiple(statement, parameters, CreateGRM);
        }
        private GoalReferenceMap CreateGRM(SQLiteDataReader rd)
        {
            var result = new GoalReferenceMap();
            result.GoalReferenceMapID = (long)rd["GoalReferenceMapID"];
            result.BaseGoal = GoalReader.ReadByID((long)rd["BaseGoalID"]);
            result.ReferencedGoal = GoalReader.ReadByID((long)rd["TargetGoalID"]);
            
            return result;
        }
    }
}
